//
//  NetTools.swift
//  02-封装AFN
//
//  Created by 牛威 on 15/12/7.
//  Copyright © 2015年 牛威. All rights reserved.
//

import UIKit

import AFNetworking

//定义枚举

enum NWRequestMethod : String{

    case GET = "GET"
    
    case POST = "POST"


}

//MARK: - oauth应用拼接信息


private let appKey = "1081648050"
private let appSecret = "7b90de06f71695350f27473f41cf0744"
private let rediecturl = "http://www.baidu.com"

//MARK: - 单例的创建
class NetTools: AFHTTPSessionManager {
    
    
    static let shared : NetTools = {
    
       let tools = NetTools(baseURL: nil)
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
//        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        return tools

    
    }()
    
    
    //进行token的字典处理
    
    var tokenDict : [String : AnyObject]? {
    
    
        if let token = UserAccountViewModel.sharedAccount.accesstToken {
        
         
            return["access_token" : token]
        
        
        }
    
           return nil
    }
    

}


//MARK: - 加载微博数据相关方法

extension NetTools {


    func loadStatus(finshed:NWRequestCallback){
    
        //获取token字典
        
        guard let paras = tokenDict else {
            
            
            finshed(result: nil, error: NSError(domain: "cn.baidu.com", code: 0126, userInfo: ["message":"token is nil"]))
            
            return
        }

       let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        request(.GET, urlString: urlString, parameters: paras, finshed: finshed)
    
    }



}



//MARK: - 访问用户信息
extension NetTools {

    //https://api.weibo.com/2/users/show.json access_token uid
    // - see: [http://open.weibo.com/wiki/2/users/show](http://open.weibo.com/wiki/2/users/show)

    func loadUserInfo(Uid uid:String,finshed:NWRequestCallback){
    
        
        //获取token字典
        
        guard var paras = tokenDict else {
        

            finshed(result: nil, error: NSError(domain: "cn.baidu.com", code: 0126, userInfo: ["message":"token is nil"]))
            
            return
        }
        
        paras["uid"] = uid
     
        let urlString = "https://api.weibo.com/2/users/show.json"
        
    
        request(.GET, urlString: urlString, parameters: paras, finshed: finshed)
    
    
    }


}




//拼接oauth连接

extension NetTools {
    
    //

   /// - see: [wwww.baidu.com](wwww.baidu.com)
    var oauthURL:NSURL  {
        
        
    //https://api.weibo.com/oauth2/authorize?
    
let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(appKey)&redirect_uri=\(rediecturl)"
        
        return NSURL(string: urlString)!
    
    
    }
    
    
    //MARK: - 加载accessToken
    
    //swift中的起别名  typealias 定义的别名首字母有要大写 其他不需要注意
    
    typealias NWRequestCallback = (result:AnyObject?,error:NSError?)->()
    
    
    
    
    func loadAccessToken(code:String,finshed:NWRequestCallback){
        
        
        //https://api.weibo.com/oauth2/access_token

 
        let urlString = "https://api.weibo.com/oauth2/access_token"
    
    
      let paras = [ "client_id":appKey,
                    "client_secret":appSecret,
                    "grant_type":"authorization_code",
                    "code":code,
                    "redirect_uri":rediecturl]
        
        
        request(.POST, urlString: urlString, parameters: paras, finshed: finshed)
        
    }
}




//MARK: - 封装网络工具 集成这中网络框架的时候方法应该受保护 是私有的所以加上private


extension NetTools {


  private  func request(method:NWRequestMethod, urlString:String,parameters:[String : AnyObject]?,finshed:NWRequestCallback){
        
        let success = {(task :NSURLSessionDataTask, result:AnyObject) -> Void in
        
            finshed(result: result, error: nil)

        
        }
        
        let failure = {(task:NSURLSessionDataTask?,error: NSError) -> Void in
        
            finshed(result: nil, error: error)

        
        }
        
        
        
        if method == NWRequestMethod.GET {
        
          GET(urlString, parameters: parameters, success: success, failure: failure)
            
        
        
        }else {
        
          POST(urlString, parameters: parameters, success: success, failure: failure)
        
        }
    
    }
}
